package floodfill;


public class Exercises7 {
    // LCR 130、衣橱整理
    int m;
    int n;
    int cnt;
    int[] dx = {0,0,1,-1};
    int[] dy = {1,-1,0,0};
    boolean[][] visit; //标记整理过的格子
    int ret;

    public int wardrobeFinishing(int m1, int n1, int cnt1) {
        m = m1;
        n = n1;
        cnt = cnt1;
        visit = new boolean[m][n];
        dfs(0,0);
        return ret;
    }

    public void dfs(int i,int j) {
        ret++;
        visit[i][j] = true;

        for(int k = 0;k < 4;k++) {
            int x = i + dx[k];
            int y = j + dy[k];
            if(x >= 0 && x < m && y >= 0 && y < n && !visit[x][y] && check(x,y)) {
                dfs(x,y);
            }
        }
    }

    public boolean check(int x,int y) {
        int sum = 0;
        while(x != 0) {
            sum += (x % 10);
            x /= 10;
        }
        while(y != 0) {
            sum += (y%10);
            y /= 10;
        }

        return sum <= cnt;
    }
}
